<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12" >
            <a-form-item label="商品名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['productName']" placeholder="请输入商品名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="日文名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['productOtherName']" placeholder="请输入日文名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="货号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['productCode']" placeholder="请输入货号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="商品类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['productType']" :trigger-change="true" dictCode="product_type" placeholder="请选择商品类别"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="原价" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['originalPrice', validatorRules.originalPrice]" placeholder="请输入原价" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="现价" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['price', validatorRules.price]" placeholder="请输入现价" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="序号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['seq', validatorRules.seq]" placeholder="请输入序号" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['remark']" placeholder="请输入备注"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="销售数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['saleNum', validatorRules.saleNum]" placeholder="请输入销售数量" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="评论数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['commentNum', validatorRules.commentNum]" placeholder="请输入评论数量" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="是否热销" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!--<a-input v-decorator="['hotSale']" placeholder="请输入是否热销"></a-input>-->
              <j-dict-select-tag type="list" v-decorator="['hotSale']" :trigger-change="true" dictCode="yn" placeholder="请选择是否热销"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="是否折扣" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!--<a-input v-decorator="['discount']" placeholder="请输入是否折扣"></a-input>-->
              <j-dict-select-tag type="list" v-decorator="['discount']" :trigger-change="true" dictCode="yn" placeholder="请选择是否折扣"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="是否新品" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!--<a-input v-decorator="['newProduct']" placeholder="请输入是否新品"></a-input>-->
              <j-dict-select-tag type="list" v-decorator="['newProduct']" :trigger-change="true" dictCode="yn" placeholder="请选择是否新品"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="商品活动" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['ruleId']" :trigger-change="true" dictCode="product_rule" placeholder="请输入商品活动" onchange="changeRule"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" id="ruleOffId" hidden>
            <a-form-item label="满减规则" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['ruleOffId']" :trigger-change="true" dictCode="rs_shop_rule,name,id" placeholder="请选择满减规则"/>
            </a-form-item>
          </a-col>
          <a-col :span="12" >
            <a-form-item label="模板" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['mouldId']" :trigger-change="true" dictCode="rs_mould,name,id" placeholder="请选择模板样式"/>
            </a-form-item>
          </a-col>

          <a-col :span="12" >
            <a-form-item label="站点" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['siteManage']" :trigger-change="true" dictCode="site_manage" placeholder="请选择站点"/>
            </a-form-item>
          </a-col>

        </a-row>
      </a-form>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="产品尺码" :key="refKeys[0]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[0]"
          :loading="rsProductSizeTable.loading"
          :columns="rsProductSizeTable.columns"
          :dataSource="rsProductSizeTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
      <a-tab-pane tab="特价表" :key="refKeys[1]" :forceRender="true" id="tejia">
        <j-editable-table
          :ref="refKeys[1]"
          :loading="rsShopBargainTable.loading"
          :columns="rsShopBargainTable.columns"
          :dataSource="rsShopBargainTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
      <a-tab-pane tab="商品图片" :key="refKeys[2]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[2]"
          :loading="rsShopImageTable.loading"
          :columns="rsShopImageTable.columns"
          :dataSource="rsShopImageTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
      <a-tab-pane tab="商品评论" :key="refKeys[3]" :forceRender="true">
        <j-editable-table
          :ref="refKeys[3]"
          :loading="rsShopCommentTable.loading"
          :columns="rsShopCommentTable.columns"
          :dataSource="rsShopCommentTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="true"
          :actionButton="true"/>
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import pick from 'lodash.pick'
  import { getAction } from '@/api/manage'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: 'RsProductForm',
    mixins: [JEditableTableMixin],
    components: {
      JFormContainer,
      JDictSelectTag,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          originalPrice: {
            rules: [
              { required: false},
              { pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
            ]
          },
          price: {
            rules: [
              { required: false},
              { pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
            ]
          },
          seq: {
            rules: [
              { required: false},
              { pattern: /^-?\d+$/, message: '请输入整数!'},
            ]
          },
          saleNum: {
            rules: [
              { required: false},
              { pattern: /^-?\d+$/, message: '请输入整数!'},
            ]
          },
          commentNum: {
            rules: [
              { required: false},
              { pattern: /^-?\d+$/, message: '请输入整数!'},
            ]
          },
        },
        refKeys: ['rsProductSize', 'rsShopBargain', 'rsShopImage', 'rsShopComment', ],
        tableKeys:['rsProductSize', 'rsShopBargain', 'rsShopImage', 'rsShopComment', ],
        activeKey: 'rsProductSize',
        // 产品尺码
        rsProductSizeTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '尺码名称',
              key: 'name',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '序号',
              key: 'seq',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        // 特价表
        rsShopBargainTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '商品单位',
              key: 'productUnit',
              type: FormTypes.select,
              dictCode:"product_unit",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '商品型号',
              key: 'productSpec',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '数量',
              key: 'num',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              // validateRules: [{ pattern: "z", message: "${title}格式不正确" }],
            },
            {
              title: '金额',
              key: 'money',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              // validateRules: [{ pattern: "money", message: "${title}格式不正确" }],
            },
          ]
        },
        // 商品图片
        rsShopImageTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '图片类型',
              key: 'imageType',
              type: FormTypes.select,
              dictCode:"image_type",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              // validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '中文颜色',
              key: 'title',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '颜色名称',
              key: 'otherTitle',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '图片路径',
              key: 'url',
              type: FormTypes.file,
              token:true,
              responseName:"message",
              width:"200px",
              placeholder: '请选择文件',
              defaultValue: '',
              // validateRules: [{ required: true, message: '${title}不能为空' }],
            },
          ]
        },
        // 商品评论
        rsShopCommentTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '评论内容',
              key: 'comment',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              // validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '评论人',
              key: 'commentUser',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '评论时间',
              key: 'commentTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
              // validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '满意度',
              key: 'star',
              type: FormTypes.inputNumber,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        url: {
          add: "/shop/rsProduct/add",
          edit: "/shop/rsProduct/edit",
          queryById: "/shop/rsProduct/queryById",
          rsProductSize: {
            list: '/shop/rsProduct/queryRsProductSizeByMainId'
          },
          rsShopBargain: {
            list: '/shop/rsProduct/queryRsShopBargainByMainId'
          },
          rsShopImage: {
            list: '/shop/rsProduct/queryRsShopImageByMainId'
          },
          rsShopComment: {
            list: '/shop/rsProduct/queryRsShopCommentByMainId'
          },
        }
      }
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：false流程表单 true普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          })
        }
      },
      addBefore(){
        this.form.resetFields()
        this.rsProductSizeTable.dataSource=[]
        this.rsShopBargainTable.dataSource=[]
        this.rsShopImageTable.dataSource=[]
        this.rsShopCommentTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'productName','productOtherName','productCode','productType','originalPrice','price','status','seq','remark','saleNum','commentNum','hotSale','discount','newProduct','ruleId','ruleName','onTime')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.rsProductSize.list, params, this.rsProductSizeTable)
          this.requestSubTableData(this.url.rsShopBargain.list, params, this.rsShopBargainTable)
          this.requestSubTableData(this.url.rsShopImage.list, params, this.rsShopImageTable)
          this.requestSubTableData(this.url.rsShopComment.list, params, this.rsShopCommentTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          rsProductSizeList: allValues.tablesValue[0].values,
          rsShopBargainList: allValues.tablesValue[1].values,
          rsShopImageList: allValues.tablesValue[2].values,
          rsShopCommentList: allValues.tablesValue[3].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'productName','productOtherName','productCode','productType','originalPrice','price','status','seq','remark','saleNum','commentNum','hotSale','discount','newProduct','ruleId','ruleName','onTime'))
     },
//改变商品的活动，改变规则
      changeRule(){
        if(this.roleId=='4'){  //满减规则
          $('#ruleOffId').show();
        }
        if(this.roleId=='5'){  //阶梯规则
          $('#').show();
        }
        if(this.roleId=='6'){  //特价
          $('#tejia').show();
        }
      }
    }
  }
</script>

<style scoped>
</style>